import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

/**
 * @author QL
 * @date 2021-12-24 13:58:17
 * @version V..
 * @description 处理路由push方法，解决重复触发push相同路由报异常的问题
 */
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

/**
 * @author QL
 * @param {Object} mainViewComponents 主页面级容器组件对象的存储字典 例如home页面
 * @param {Object} subViewComponents 子页面级容器组件对象的存储字典 例如subView1页面
 * @date 2021-12-24 13:58:30
 * @version V..
 * @description 扫描指定文件夹，获取组件对象，以组件name为key，组件实例default对象/根实例对象为value存入相应的Components 字典中
 */
const mainViewComponents = {};
const contextMainView = require.context('@/views');

const subViewComponents = {};
const contextSubView = require.context('@/components/subViewCom');

contextMainView.keys().forEach(path => {
	if (contextMainView(path).default) {
		mainViewComponents[contextMainView(path).default.name] = contextMainView(path).default;
	} else {
		mainViewComponents[contextMainView(path).name] = contextMainView(path);
	}
})

contextSubView.keys().forEach((path) => {
	if (contextSubView(path).default) {
		subViewComponents[contextSubView(path).default.name] = contextSubView(path).default;
	} else {
		subViewComponents[contextSubView(path).name] = contextSubView(path);
	}
})


/**
 * @author QL
 * @date 2021-12-24 13:59:02
 * @version V..
 * @description router插件的配置列表，用于创建router对象
 */
const routes = [
	{
		path: "/",
		name: "loadpath",
		meta: {
			// 配置网页页签名称
			title: "web项目骨架过渡页重定向"
		},
		redirect: '/login',
	},
	{
		path: "/login",
		name: "loginPage",
		meta: {
			// 配置网页页签名称
			title: "登录页"
		},
		component: mainViewComponents.loginPage,
	},
	{
		path: "/home",
		name: "mainContainer",
		meta: {
			// 配置网页页签名称
			title: "首页"
		},
		component: mainViewComponents.mainContainer,

		redirect: '/home/subView1',
		children: [
			{
				path: 'subView1',
				name: 'subView1',
				meta: {
					title: '测试单个post'
				},
				component: subViewComponents.subView1,
			},
			{
				path: 'subView2',
				name: 'subView2',
				meta: {
					title: '测试promisAll同步回收'
				},
				component: subViewComponents.subView2,
			},
			{
				path: 'subView3',
				name: 'subView3',
				meta: {
					title: '子页3'
				},
				component: subViewComponents.subView3,
			},
			{
				path: 'arcMap',
				name: 'arcMap',
				meta: {
					title: '地图测试'
				},
				component: subViewComponents.ocnGisMap,
			},
			{
				path: 'olMap',
				name: 'olMap',
				meta: {
					title: '地图测试'
				},
				component: subViewComponents.olMap,
			},
			{
				path: 'storeTest',
				name: 'storeTest',
				meta: {
					title: 'vuex状态测试'
				},
				component: subViewComponents.storeTest,
			},
			{
				path: 'echartsTest',
				name: 'echartsTest',
				meta: {
					title: 'echarts测试'
				},
				component: subViewComponents.echartsTest,
			},
			{
				path: 'dialogTest',
				name: 'dialogTest',
				meta: {
					title: 'dialog组件测试'
				},
				component: subViewComponents.dialogTest,
			},
			{
				path: 'formTest',
				name: 'formTest',
				meta: {
					title: '定制表单测试'
				},
				component: subViewComponents.formTest,
			},
			{
				path: 'tableTest',
				name: 'tableTest',
				meta: {
					title: '定制列表测试'
				},
				component: subViewComponents.tableTest,
			},
			{
				path: 'videoTest',
				name: 'videoTest',
				meta: {
					title: 'easyPlayer测试'
				},
				component: subViewComponents.videoTest,
			}
		]
	}
];

const router = new VueRouter({
	routes
})

/**
 * @author QL
 * @date 2021-12-24 13:59:21
 * @version V..
 * @description 该配置与router组件的路由（routes）配置项meta属性的title关联使用, 用于自定义不同路由的网页页签名称,采用了路由守卫 beforeEach 方法拦截并替换页签名称
 */
router.beforeEach((to, from, next) => {
	/* 路由发生变化修改页面title */
	if (to.meta.title) {
		document.title = `骨架项目-${to.meta.title}`;
	}
	next()
})

export default router
